﻿#ifndef DISPATCHCORE_H
#define DISPATCHCORE_H

#include <iostream>
#include <QObject>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QtMath>
#include <QMap>
#include <QTimer>
#include <QImage>
#include <algorithm> // 包含find_if算法
#include <functional> // 包含bind函数对象适配器
#include <QWebSocketServer>
#include <QWebSocket>
#include "./MotorManager/KincoServoManager.h"
#include "Base/Command.h"
#include "Base/Model.h"
#include "Monitor/ServerUdpMonitor.h"
#include "Manager/HttpClientManager.h"
#include "Sqlite/DeviceDBManager.h"

using namespace std;
class PortStateMonitor;
class DispatchCore: public QObject
{
    Q_OBJECT
public:
    explicit DispatchCore(QObject *parent = nullptr);
    ~DispatchCore();
    bool distributePort(int standard, int &pos);
    string QJson2StdString(QJsonObject json);
signals:
    void sig_setParamsResult(QJsonObject);
    void sig_ParamsResult(QJsonObject);
    void sig_storgeUIRefreshTable();
    void sig_addCmdToTable(InOutCabCommand_Server);
    void sig_freshChartView(DevArvStatisticsModel);
    void sig_freshPortInfo(QVector<PortGoodsIndicator> portgoods,QList<InOutCabCommand_Server>);
    void sig_freshUserInfo(QList<QVariantList>);
    void sig_SearchInfoReceived(PagedResultDto<DevArvData>);
    void sig_imgCmdResult(QImage);
private slots:
    void slot_sendCmd2LinuxClient(QJsonObject);
    void slot_CoreInit();
    void slot_setParams(int deviceno,int objectindex,int value);
    void slot_setControl(uint8_t deviceno,uint8_t value);
    void slot_setProbe(quint8 deviceno,quint8 type);
    void slot_moveCmd(QString type,int value1,int value2,QString word);
    void slot_imgCmd();
    void slot_inOutCabCmd(InOutCabCommand_Board cmd);
    void slot_sendQRLocateCmd(QRLocateCommand);
    void slot_portMessageProcess(QVector<bool>);
    void slot_subConfigInsertDB(QList<QVariantList>);
    void slot_portConfigInsertDB(QList<QVariantList>);
    void slot_deviceConfigDB(QList<QVariantList>);
    void slot_goodsDistributeCalculate();
    void slot_ServerCommandAnalysis(QJsonObject data);
    void slot_cmdDelete(int index,QString qrcode);
    void slot_cmdClear();
    void slot_getDevArvStatistics();
    void slot_userInfoInsertDB(QVariantList);
    void slot_getUserInfo();
    void slot_updateUserInfo(QVariantMap,QVariantMap);
    void slot_deleteUserInfo(QVariantMap);
    void slot_sendSearchPost(QVariantMap params);
    void slot_addCmdFromPad(QList<SearchCmdModel> cmdlist);
    void slot_newWsConnection();
    void slot_wsdisconnected();
    void slot_textMessageReceived(const QString &message);
    void slot_binaryMessageReceived(const QByteArray &message);
    void slot_taskTimerTimeout();
public:
    void recieveDataFromClient(QJsonObject data);
    void QRLocateResultProcess(QRLocateCommandResponse response);
    void InOutCabCmdResultProcess(InOutCabCommandResponse response);
    int goodsCostCalculate(int x,int y);
    void configArvInOutCmd(QString cmdType, QList<QVariant> rfidData);
    inline bool isSameInOutCabCmd(InOutCabCommand_Server cmd);
private:
    QWebSocketServer *m_pWebSocketServer {nullptr};
    QWebSocket *m_client  {nullptr};

    KincoServoManager *pKincoServoManager {nullptr};
    ServerUdpMonitor *pServerUdpMonitor {nullptr};

    PortStateMonitor *pPortStateMonitor {nullptr};
    QThread *pThread_PortStateMonitor {nullptr};

    QTimer* taskTimer {nullptr};

    QVector<PortGoodsIndicator> mPortOccupy;
    QList<InOutCabCommand_Server> mServerCommand;
    QString mCurrentInOutCmd = NULL;
    int mDeviceNo = DeviceDBManager::getInstance()->getDeviceInfo("DeviceNo").toInt();
};

#endif // DISPATCHCORE_H
